﻿using System;

namespace MyAMS
{
    /// <summary>
    /// 基组件
    /// </summary>
    public abstract class BaseComponent : IComponent
    {
        private bool isDisposed;

        #region IComponent Members

        /// <summary>
        /// 获取一个值, 改制指示当前实例是否已被初始化
        /// </summary>
        /// <value></value>
        public bool IsInitialized { get; private set; }

        /// <summary>
        /// 应用程序
        /// </summary>
        /// <value></value>
        public IApplication Application { get; private set; }

        /// <summary>
        /// 初始化
        /// </summary>
        /// <param name="application">应用程序</param>
        public void Initialize(IApplication application)
        {
            if (!IsInitialized)
            {
                Application = application;
                OnInitialize();
                IsInitialized = true;
            }
        }

        #endregion

        #region IDisposable Members

        /// <summary>
        /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
        /// </summary>
        public void Dispose()
        {
            if (!isDisposed && IsInitialized)
            {
                isDisposed = true;
                OnDispose(true);
                IsInitialized = false;
                Application = null;
            }
            GC.SuppressFinalize(this);
        }

        #endregion

        /// <summary>
        /// 初始化时调用
        /// </summary>
        protected abstract void OnInitialize();

        /// <summary>
        /// 销毁时调用
        /// </summary>
        /// <param name="isDisposing">是否显式发起</param>
        protected abstract void OnDispose(bool isDisposing);
    }
}
